/**
 * 
 */

init();
var userId=0;

function init(){
	$.ajax({
		type:'post',
		url:'http://localhost:8088/myOA/UserInfo',
		data:{
			"typeName":"UserInfo",
			"method":"getPageDatas"
		},
		success:function(data){
			var trs="";
			var json=JSON.parse(data);
//			console.log(json);
			for(var i=0;i<json.length;i++){				
				trs+="<tr><td>"+json[i].userId+"</td>";					
				trs+="<td>"+json[i].userName+"</td>";
				trs+="<td>"+json[i].pwd+"</td>";
				trs+="<td>"+json[i].roleId+"</td>";
				trs+="<td><a href='http://localhost:8088/myOA/jsp/hr/manage_hr_staff_detail.jsp'>详情</a></td>";
				trs+="<td hidden>"+json[i].departId+"</td>";
				trs+="<td hidden>"+json[i].gender+"</td>";
				trs+="<td hidden>"+json[i].userState+"</td>";			
				trs+="<td><button onclick='modify(this)'>修改</button></td>";
				trs+="<td><button onclick='remove(this)'>删除</button></td></tr>";
			}
			$("#division tbody").empty().append(trs);
		},
		error:function(res){
			console.log(res);
		}
	});	
	
	
}

//展示添加窗口
$("#addStaff").click(function(){
	$("#addStaffForm").css({
		"top": "10vh",
		"transition": "top .3s ease",
	});
	$("#cover").css("display", "block").attr("onclick", "coverBack('#addStaffForm')");
});

//添加保存

$("#addStaffFormBtn").click(function(){
	var staffInfo=[];
	staffInfo.length=0;
	var staffInfos=$(".staffInfo");
//	console.log(staffInfos);
	for(var i=0;i<staffInfos.length;i++){
		staffInfo.push(staffInfos.eq(i).val());
	}
//	console.log(staffInfo);
	var gender=$('input[name="gender"]:checked').val();
	
	var staff={
			"userName":staffInfo[0],
			"pwd":staffInfo[1],
			"departId":staffInfo[2],
			"gender":gender,
			"roleId":staffInfo[3],
			"userState":staffInfo[4]
	}
//	console.log(data);
	$.ajax({
		type:'post',
		url:'http://localhost:8088/myOA/UserInfo',
		data:{
			"typeName":"UserInfo",
			"method":"add",
			"data":JSON.stringify(staff)
		},
		success:function(data){
			if(data!="true"){
				alert("添加失败");
			}else{
				alert("添加成功");
			}
			init();
			coverBack($("#addStaffForm"));
		},
		error:function(res){
			console.log("失败");
		}
	});
});

//展示修改窗体
function modify(btn){
	var gender=$(btn).parents('tr').eq(0).children().eq(6).text();
	userId=$(btn).parents('tr').eq(0).children().eq(0).text();
	var tds=$(btn).parents('tr').eq(0).children();
	var datas=[];
	for(var i=0;i<tds.length;i++){
		datas.push(tds.eq(i).text());
	}
//	console.log(datas);
	var staffInfos=$(".staffInfoModify");
	
	staffInfos.eq(0).val(datas[1]);
	staffInfos.eq(1).val(datas[2]);
	staffInfos.eq(2).val(datas[5]);
	if(gender==1){
		$("#male").prop("checked",true);
		$("#female").prop("checked",false);
	}	
	if(gender==0){
		$("#male").prop("checked",false);
		$("#female").prop("checked",true);
	}
	staffInfos.eq(3).val(datas[3]);
	staffInfos.eq(4).val(datas[7]);
	
	$("#modifyStaffForm").css({
		"top": "10vh",
		"transition": "top .3s ease",
	});
	$("#cover").css("display", "block").attr("onclick", "coverBack('#modifyStaffForm')");	
}

//保存修改
$("#modifyStaffFormBtn").click(function(){
	var gender=$('.staffGenderModify[name="gender"]:checked').val();
	var staffInfos=$(".staffInfoModify");
	
	var staff={
			"userName":staffInfos[0].value,
			"pwd":staffInfos[1].value,
			"departId":staffInfos[2].value,
			"gender":gender,
			"roleId":staffInfos[3].value,
			"userState":staffInfos[4].value,
			"userId":userId
	}
//	console.log(staff);
//	console.log(data);
	$.ajax({
		type:'post',
		url:'http://localhost:8088/myOA/UserInfo',
		data:{
			"typeName":"UserInfo",
			"method":"update",
			"data":JSON.stringify(staff)
		},
		success:function(data){
			if(data!="true"){
				alert("修改失败");
			}else{
				alert("修改成功");
			}
			init();
			coverBack($("#modifyStaffForm"));
		},
		error:function(res){
			console.log("失败");
		}
	});
});


//删除
function remove(btn){
	if(confirm("是否删除?")){
		var id=$(btn).parents('tr').eq(0).children().eq(0).text();
		var data={
				"userId":id
		}
		$.ajax({
			type:'post',
			url:'http://localhost:8088/myOA/UserInfo',
			data:{
				"typeName":"UserInfo",
				"method":"delete",
				"data":JSON.stringify(data)
			},
			success:function(data){
				if(data!="true"){
					alert("删除失败");
				}else{
					alert("删除成功");
				}
				init();	
			},
			error:function(res){
				console.log("网络错误");
			}
		});
	}
}


